package com.kid.springcloud.controller;

import com.kid.springcloud.entity.Member;
import com.kid.springcloud.entity.Result;
import com.kid.springcloud.service.MemberOpenFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class MemberNacosConsumerController {

    //    定义MEMBER_SERVICE_PROVIDER_URL，作为基础地址
    public static final String MEMBER_SERVICE_NACOS_PROVIDER_URL="http://member-service-nacos-provider";

    @Resource
    private RestTemplate restTemplate;

    //    装配openfeign service
    @Resource
    private MemberOpenFeignService memberOpenFeignService;

    @Resource
    private DiscoveryClient discoveryClient;

//    通过openfeign完成远程调用
    @GetMapping("/member/openfeign/consumer/get/{id}")
    public Result<Member> getMemberOpenFeignById(@PathVariable("id") Long id) {
        return memberOpenFeignService.queryById(id);
    }
    @PostMapping("/member/nacos/consumer/save")
    public Result<Member> save(Member member){
        return restTemplate.postForObject(MEMBER_SERVICE_NACOS_PROVIDER_URL+"/member/save",member,Result.class);
    }


    @GetMapping("/member/nacos/consumer/get/{id}")
    public Result<Member> getMemberById(@PathVariable("id") Long id){
        return restTemplate.getForObject(MEMBER_SERVICE_NACOS_PROVIDER_URL+"/member/get/"+id,Result.class);
    }

}
